package com.sokal.easycare.modules.sysparams.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.sys.service.SystemService;
import com.sokal.easycare.modules.sysparams.entity.SysParams;
import com.sokal.easycare.modules.sysparams.service.SysParamsService;

/**
 * 参数配置，用于配置系统中统一格式参数内容Controller
 * @author lihaipei
 * @version 2017-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/sysparams/sysParams")
public class SysParamsController extends BaseController {

	@Autowired
	private SysParamsService sysParamsService;
	
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public SysParams get(@RequestParam(required=false) String id) {
		SysParams entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysParamsService.get(id);
		}
		if (entity == null){
			entity = new SysParams();
		}
		return entity;
	}
	
	@RequiresPermissions("sysparams:sysParams:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysParams sysParams, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysParams> page = sysParamsService.findPage(new Page<SysParams>(request, response), sysParams); 
		model.addAttribute("page", page);
		model.addAttribute("allRoles", systemService.findAllRole());
		return "modules/sysparams/sysParamsList";
	}

	@RequiresPermissions("sysparams:sysParams:view")
	@RequestMapping(value = "form")
	public String form(SysParams sysParams, Model model) {
		model.addAttribute("sysParams", sysParams);
		model.addAttribute("allRoles", systemService.findAllRole());
		return "modules/sysparams/sysParamsForm";
	}

	@RequiresPermissions("sysparams:sysParams:edit")
	@RequestMapping(value = "save")
	public String save(SysParams sysParams, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysParams)){
			return form(sysParams, model);
		}
		sysParamsService.save(sysParams);
		addMessage(redirectAttributes, "保存参数配置成功");
		return "redirect:"+Global.getAdminPath()+"/sysparams/sysParams/?repage";
	}
	
	@RequiresPermissions("sysparams:sysParams:edit")
	@RequestMapping(value = "delete")
	public String delete(SysParams sysParams, RedirectAttributes redirectAttributes) {
		sysParamsService.delete(sysParams);
		addMessage(redirectAttributes, "删除参数配置成功");
		return "redirect:"+Global.getAdminPath()+"/sysparams/sysParams/?repage";
	}

	@RequiresPermissions("sysparams:sysParams:edit")
	@RequestMapping(value = "checkValue")
	@ResponseBody
	public Map<String, Object> checkValue(SysParams sysParams) {
		return sysParamsService.checkValue(sysParams);
	}
}